package com.ruoyi.flowable.listener.move;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquMove;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.ruoyi.flowable.listener.move.MoveListener_7_1.getActDuration;

/**
 * 移动-7-2-1监听
 */
@Slf4j
@Component("moveListener_7_2_1")
public class MoveListener_7_2_1 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            EquMove equMove = execution.getVariable("equMove", EquMove.class);
            //执行信息
            Map<String, Object> executeInfoMap = objectMapper.readValue(equMove.getExecuteInfo(), new TypeReference<Map<String, Object>>() {});
            HashMap<String, Object> dataMap = new HashMap<>();
            String situation7_2 = execution.getVariable("situation7_2", String.class);
            dataMap.put("设备状态", situation7_2);
            String detailSituation7_2 = execution.getVariable("detailSituation7_2", String.class);
            dataMap.put("损伤描述", detailSituation7_2);
            String moveTime7_2 = execution.getVariable("moveTime7_2", String.class);
            dataMap.put("移动时间", moveTime7_2);
            String completeTime7_2 = execution.getVariable("completeTime7_2", String.class);
            dataMap.put("完成时间", completeTime7_2);
            executeInfoMap.put("移出科室归还验收", dataMap);
            //计算实际移动时长(小时)
            String moveTime7_1 = execution.getVariable("moveTime7_1", String.class);
            BigDecimal actDuration = getActDuration(moveTime7_1, completeTime7_2);
            executeInfoMap.put("实际移动时长", actDuration);
            String executeInfo = objectMapper.writeValueAsString(executeInfoMap);

            //移动后状态:设备状态+损伤描述<如有>+新归属科室/存放地点
            Map<String, Object> moveAfterStatusMap = objectMapper.readValue(equMove.getMoveAfterStatus(), new TypeReference<Map<String, Object>>() {});
            HashMap<String, Object> valueMap = new HashMap<>();
            valueMap.put("设备状态", situation7_2);
            valueMap.put("损伤描述", detailSituation7_2);
            valueMap.put("新归属科室", execution.getVariable("fromDept", String.class));
            valueMap.put("存放地点", execution.getVariable("fromLocation", String.class));
            moveAfterStatusMap.put("移出科室归还验收", valueMap);
            String moveAfterStatus = objectMapper.writeValueAsString(moveAfterStatusMap);

            equMove.setExecuteInfo(executeInfo);
            equMove.setMoveAfterStatus(moveAfterStatus);
            execution.setVariable("equMove", equMove);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
